﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
 using System.Collections;
using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin
{
    public class BaseAdmin : System.Web.UI.Page
    {
       
        readonly adminlogin theAdminLogin = new adminlogin();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();
        public string AdminUser
        {
            get
            {
               
                if (this.Session["Adminlogin"] != null)
                {
                    return this.Session["Adminlogin"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public string AdminUserId
        {
            get
            {
                if (this.Session["AdminUserId"] != null)
                {
                    return this.Session["AdminUserId"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public string AdminTypeId
        {
            get
            {
                if (this.Session["AdminTypeId"] != null)
                {
                    return this.Session["AdminTypeId"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        public string AdminShopId
        {
            get
            {
                if (this.Session["AdminShopId"] != null)
                {
                    return this.Session["AdminShopId"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public string CompanyId
        {
            get
            {
                if (this.Session["CompanyId"] != null && this.Session["CompanyId"].ToString() != "")
                {
                    return this.Session["CompanyId"].ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["CompanyId"] = value;
            }
        }

        public string CompanyLevelId
        {
            get
            {
                if (this.Session["CompanyLevelId"] != null)
                {
                    return this.Session["CompanyLevelId"].ToString();
                }
                else
                {
                    return "0";
                }
            }
            set
            {
                this.Session["CompanyLevel"] = value;
            }
        }

        public string OrderId
        {
            get
            {
                if (this.Session["OrderId"] != null)
                {
                    return this.Session["OrderId"].ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["OrderId"] = value;
            }
        }


        public List<UserCard> UserCardCodeList
        {
            get
            {
                if (Session["UserCardCodeList"] != null)
                {
                    return (List<UserCard>)Session["UserCardCodeList"];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                Session["UserCardCodeList"] = value;
            }
        }

        public Hashtable HtLoginAdmin
        {
            get
            {
                if (HttpContext.Current.Cache["LoginAdmin"] != null)
                {
                    return HttpContext.Current.Cache["LoginAdmin"] as System.Collections.Hashtable;
                }
                else
                    return null;
            }
            set
            {
                HttpContext.Current.Cache["LoginAdmin"] = value;
            }

        }





        protected void Page_Init(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='/admin/login.aspx?callid=" + Request.QueryString["calluser"] + "&callpas=" + Request.QueryString["callpas"] + "';</script>");
                base.Response.End();
            }
            else
            {
                if (HtLoginAdmin != null && HtLoginAdmin.Contains(AdminUserId))
                {
                    if (HtLoginAdmin[AdminUserId].ToString() != HttpContext.Current.Request.UserHostAddress)
                    {
                        Session.Clear();
                        base.Response.Write("<script language=javascript>window.top.location.href='/admin/login.aspx?msg=otherlogin';</script>");
                        base.Response.End();
                    }
                }
            }
        }

        public void ValidateLoginRule(ERule oRule)
        {
           int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
           DataSet ds;
           bool bValidate = false;
           ds = theUserRuleRef.GetListByAdminId(iAdminId);
           if (ds != null && ds.Tables.Count > 0)
           {
               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["Name"].ToString() == oRule.ToString())
                   {
                       bValidate = true;
                       break;
                   }
               }
           }

           ds = theUserRuleRef.GetListByAdminIdForCompany(iAdminId);
           if (ds != null && ds.Tables.Count > 0)
           {
               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["CompanyRule"].ToString() == oRule.ToString())
                   {
                       bValidate = true;
                       break;
                   }
               }
           }
           if (!bValidate)
           {
               base.Response.Write("<script language=javascript>window.top.location.href='/admin/login.aspx';</script>");
               base.Response.End();
           }
        }

       void ValidatePageLoginRule(ERule oPageRule)
        {
            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);
            bool isValidatePage = false;
            DataSet ds;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                    if (oERule == oPageRule)
                    {
                        break;
                    }
                }
            }
            if (!isValidatePage)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }

        }

       public bool GetValidateLoginRule(ERule oRule)
       {
           int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
           DataSet ds;
           bool bValidate = false;
           ds = theUserRuleRef.GetListByAdminId(iAdminId);
           if (ds != null && ds.Tables.Count > 0)
           {
               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["Name"].ToString() == oRule.ToString())
                   {
                       bValidate = true;
                       break;
                   }
               }
           }

           ds = theUserRuleRef.GetListByAdminIdForCompany(iAdminId);
           if (ds != null && ds.Tables.Count > 0)
           {
               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["CompanyRule"].ToString() == oRule.ToString())
                   {
                       bValidate = true;
                       break;
                   }
               }
           }
           if (!bValidate)
           {
               return false;
           }
           return true;
       }

     protected  DataTable GetAllCompany(DataTable dtSource, DataTable dtTarget, int parentId,bool isViewAllDownCompany)
       {
           DataRow drTaget;
           if (dtTarget == null)
           {
               dtTarget = dtSource.Clone();

                   for (int i = 0; i < dtSource.Rows.Count; i++)
                   {
                       if (dtSource.Rows[i]["Id"].ToString() == parentId.ToString())
                       {
                           drTaget = dtTarget.NewRow();
                           drTaget.ItemArray = dtSource.Rows[i].ItemArray;
                           dtTarget.Rows.Add(drTaget);
                       }
                   }
           }

           for (int i = 0; i < dtSource.Rows.Count; i++)
           {
               if (dtSource.Rows[i]["ParentId"].ToString() == parentId.ToString())
               {
                   drTaget = dtTarget.NewRow();
                   drTaget.ItemArray = dtSource.Rows[i].ItemArray;
                   dtTarget.Rows.Add(drTaget);

                   if (isViewAllDownCompany)
                   {
                       for (int m = 0; m < dtSource.Rows.Count; m++)
                       {
                           if (dtSource.Rows[m]["ParentId"].ToString() == dtSource.Rows[i]["Id"].ToString())
                           {
                               drTaget = dtTarget.NewRow();
                               drTaget.ItemArray = dtSource.Rows[m].ItemArray;
                               dtTarget.Rows.Add(drTaget);

                               for (int n = 0; n < dtSource.Rows.Count; n++)
                               {
                                   if (dtSource.Rows[n]["ParentId"].ToString() == dtSource.Rows[m]["Id"].ToString())
                                   {
                                       drTaget = dtTarget.NewRow();
                                       drTaget.ItemArray = dtSource.Rows[n].ItemArray;
                                       dtTarget.Rows.Add(drTaget);

                                       for (int h = 0; h < dtSource.Rows.Count; h++)
                                       {
                                           if (dtSource.Rows[h]["ParentId"].ToString() == dtSource.Rows[n]["Id"].ToString())
                                           {
                                               drTaget = dtTarget.NewRow();
                                               drTaget.ItemArray = dtSource.Rows[h].ItemArray;
                                               dtTarget.Rows.Add(drTaget);

                                               for (int q = 0; q < dtSource.Rows.Count; q++)
                                               {
                                                   if (dtSource.Rows[q]["ParentId"].ToString() == dtSource.Rows[h]["Id"].ToString())
                                                   {
                                                       drTaget = dtTarget.NewRow();
                                                       drTaget.ItemArray = dtSource.Rows[q].ItemArray;
                                                       dtTarget.Rows.Add(drTaget);
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
          return dtTarget;


       }

    }
}